<template>
  <BasicLayout>
    <template #wrapper>
      <search-form
        :is-goods="true"
        :is-single-date="true"
        :expdrop="true"
        :report-type="formData.reportType"
        :exporting="exporting"
        @export="val => onExportReport('大区城市版报表', val,exportApi)"
        @submit="onSearchReport"
      />
      <tree-table
        :loading="loading"
        :columns="columns"
        :table-data="tableData"
        :load="loadChildNodes"
        :tree-node-prop="'hasChildNodes'"
        border
      />
    </template>
  </BasicLayout>
</template>
<script>
import SearchForm from '@/components/business/SearchForm';
import TreeTable from '@/components/common/TreeTable';
import { ReGetReport as getPerformanceReport, ReExport } from '@/api/hinApi';
import exportReportMixin from '@/components/mixins/ExportReport';
import { cityColumns as Columns } from '@/utils/columns-config/hic-header-config';

export default {
  name: 'KAliveHIVDepartmentSize',
  components: {
    SearchForm,
    TreeTable
  },
  mixins: [exportReportMixin],
  data() {
    return {
      exportApi: ReExport,
      columns: Columns,
      tableData: [],
      loading: false,
      exporting: false,
      formData: {
        export: 0, // 导出字段，1为excel,2为csv
        type: 1 // 2为大区版
        // queryMode: 'COMMODITY_TYPE_CODE',
        // reportType: 'COMMODITY_TYPE_CODE'
      }
    };
  },
  methods: {
    onSearchReport(form) {
      this.loading = true;
      this.tableData = [];
      this.$nextTick(() => {
        this.formData = {
          ...this.formData,
          ...form,
          queryMode: 'COMMODITY_TYPE_CODE'
        };
        getPerformanceReport(this.formData).then(({ data, resultCode }) => {
          if (resultCode === '0000') {
            data.forEach(item => {
              item.uuid =
                  item.id + 'HIV' + new Date().getTime();
              item.hasChildNodes = item.rowLevel < 3 ? 1 : 0;// 由rowLevel判断是否存在下级 rowLevel为3时，无下级
            });
            this.tableData = data;
            this.loading = false;
          }
        }).catch(() => {
          this.loading = false;
        });
      });
    },
    loadChildNodes(tree, treeNode, resolve) {
      const { businessId, childQueryMode, childCommodityTypeLevel, id } = tree;
      console.log(id);
      switch (childCommodityTypeLevel) {
        case 'TWO': {
          this.formData.commodityTypeLevel = 'TWO';
          this.formData.goodsCategoriesOne = [businessId];
          break;
        }
        case 'THREE': {
          this.formData.commodityTypeLevel = 'THREE';
          this.formData.goodsCategoriesTwo = [businessId];
          break;
        }
        case 'FOUR': {
          this.formData.commodityTypeLevel = 'FOUR';
          this.formData.goodsCategoriesThree = [businessId];
          break;
        }
      }
      const params = {
        ...this.formData,
        queryMode: childQueryMode,
        levels: id // 传父级ID
      };
      getPerformanceReport(params).then(({ data, resultCode }) => {
        if (resultCode === '0000') {
          data.forEach(item => {
            item.uuid =
                  item.id + 'HIV' + new Date().getTime();
            item.hasChildNodes = item.rowLevel < 3 ? 1 : 0;// 由rowLevel判断是否存在下级 rowLevel为3时，无下级
          });
          resolve(data);
        }
      });
    }
  }
};
</script>
